package cn.itrip.auth.controller;

import cn.itrip.auth.service.TokenService;
import cn.itrip.auth.service.UserService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.userinfo.ItripUserVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.ErrorCode;
import cn.itrip.common.MD5;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 瑾
 *
 */
@Controller
@RequestMapping("/api")
public class UserController {
    @Resource(name = "tokenService")
    private TokenService tokenService;

    @Resource(name = "userService")
    private UserService userService;
    /**
     * 执行邮箱注册
     */
    @RequestMapping(value = "/doregister",produces = "application/json",method = RequestMethod.POST)
    public @ResponseBody Dto doRegisterByEamil(@RequestBody  ItripUserVO vo){
        //1 邮箱验证
        //....

        ItripUser user = new ItripUser();
        user.setUserCode(vo.getUserCode());
        user.setUserName(vo.getUserName());
        user.setUserPassword(MD5.getMd5(vo.getUserPassword(),32));
        ItripUser user2 = null;
        try {
            user2 = userService.findUserByUserCode(user.getUserCode());
            if(user2!=null){
                return DtoUtil.returnFail("用户名已存在", ErrorCode.AUTH_USER_ALREADY_EXISTS);
            }else {
                //执行新增操作
                userService.itriptxCreateUserByEmail(user);
                return DtoUtil.returnSuccess();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  DtoUtil.returnFail("注册失败",ErrorCode.AUTH_UNKNOWN);
        }
    }


    /**
     * 邮箱激活
     */
    @RequestMapping(value ="/activate",method = RequestMethod.PUT,produces = "application/json")
    @ResponseBody
    public Dto doEmailActive(@RequestParam String user,@RequestParam String code) throws  Exception{
        if(userService.activateEmail(user,code)){
            return DtoUtil.returnSuccess("激活成功");
        }else {
            return DtoUtil.returnFail("激活失败",ErrorCode.AUTH_ACTIVATE_FAILED);
        }
    }


    /**
     * 执行手机注册
     */
    @RequestMapping(value = "/registerbyphone",produces = "application/json",method = RequestMethod.POST)
    public @ResponseBody Dto doRegisterByPhone(@RequestBody  ItripUserVO vo){
        //1.手机号验证..pattern...

        //2调用service
        ItripUser user = new ItripUser();
        user.setUserCode(vo.getUserCode());
        user.setUserName(vo.getUserName());
        user.setUserPassword(MD5.getMd5(vo.getUserPassword(),32));
        ItripUser user2 = null;
        try {
            user2 = userService.findUserByUserCode(user.getUserCode());
            if(user2!=null){
                return DtoUtil.returnFail("用户名已存在", ErrorCode.AUTH_USER_ALREADY_EXISTS);
            }else {
                //执行新增操作
                userService.itriptxCreateUserByPhnoe(user);
                return DtoUtil.returnSuccess();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  DtoUtil.returnFail("注册失败",ErrorCode.AUTH_UNKNOWN);
        }

    }



    /**
     * 手机短信激活
     */
    @RequestMapping(value ="/validatephone",method = RequestMethod.PUT,produces = "application/json")
    @ResponseBody
    public Dto doPhoneActive(@RequestParam String user,@RequestParam String code) throws  Exception{
        if(userService.validatePhone(user,code)){


            return DtoUtil.returnSuccess("验证成功");
        }else {
            return DtoUtil.returnFail("验证失败",ErrorCode.AUTH_ACTIVATE_FAILED);
        }
    }


    /**
     * 检查邮箱是否已存在
     */
    @RequestMapping(value="/ckusr",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto ckuser(@RequestParam String name) throws  Exception{
        ItripUser user = null;
        user = userService.findUserByUserCode(name);
        if(user!=null){
            //邮箱注册--已存在该邮箱
            return DtoUtil.returnFail("邮箱已存在", ErrorCode.AUTH_USER_ALREADY_EXISTS);
        }else {
            return DtoUtil.returnSuccess();
        }
    }
}
